package com.yuwubao.uav.tool.controller;

import com.yuwubao.framework.bean.JsonResult;
import com.yuwubao.framework.bean.PageData;
import com.yuwubao.framework.loginFilter.LoginRequired;
import com.yuwubao.uav.tool.service.ProfessorService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Min;

/**
 *专家管理
 */
@RestController
@RequestMapping("/bk")
public class ProfessorController {

    private static final Logger logger = LogManager.getLogger(ProfessorController.class);
    
    @Resource
    private ProfessorService professorService;


    @LoginRequired
    @PostMapping("/addProfessor")
    @ApiOperation(value = "新增专家")
    public JsonResult addProfessor(
            @ApiParam(value = "专家名称", required = true) @RequestParam("name") String name,
            @ApiParam(value = "名称首字母", required = true) @RequestParam("nameChar") String nameChar,
            @ApiParam(value = "专家头像") @RequestParam(value = "img",required = false) String img,
            @ApiParam(value = "所属机构ID") @RequestParam(value = "organizationId",required = false) String organizationId,
            @ApiParam(value = "职位") @RequestParam(value = "job",required = false) String job,
            @ApiParam(value = "排名") @RequestParam(value = "ranking",required = false) String ranking,
            @ApiParam(value = "教育背景") @RequestParam(value = "edu",required = false) String edu,
            @ApiParam(value = "报告") @RequestParam(value = "report",required = false) String report,
            @ApiParam(value = "论文") @RequestParam(value = "paper",required = false) String paper,
            @ApiParam(value = "著作") @RequestParam(value = "book",required = false) String book,
            @ApiParam(value = "领域ID,多个用逗号分隔") @RequestParam(value = "fieldIds",required = false) String fieldIds
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("name", name);
            pageData.put("nameChar", nameChar);
            pageData.put("img", img);
            pageData.put("organizationId", organizationId);
            pageData.put("job", job);
            pageData.put("ranking", ranking);
            pageData.put("edu", edu);
            pageData.put("report", report);
            pageData.put("paper", paper);
            pageData.put("book", book);
            pageData.put("fieldIds", fieldIds);

            professorService.addProfessor(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateProfessor")
    @ApiOperation(value = "更新专家")
    public JsonResult updateProfessor(
            @ApiParam(value = "专家ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "名称首字母", required = true) @RequestParam("nameChar") String nameChar,
            @ApiParam(value = "专家名称", required = true) @RequestParam("name") String name,
            @ApiParam(value = "专家头像") @RequestParam(value = "img",required = false) String img,
            @ApiParam(value = "所属机构ID") @RequestParam(value = "organizationId",required = false) String organizationId,
            @ApiParam(value = "职位") @RequestParam(value = "job",required = false) String job,
            @ApiParam(value = "排名") @RequestParam(value = "ranking",required = false) String ranking,
            @ApiParam(value = "教育背景") @RequestParam(value = "edu",required = false) String edu,
            @ApiParam(value = "报告") @RequestParam(value = "report",required = false) String report,
            @ApiParam(value = "论文") @RequestParam(value = "paper",required = false) String paper,
            @ApiParam(value = "著作") @RequestParam(value = "book",required = false) String book,
            @ApiParam(value = "领域ID,多个用逗号分隔") @RequestParam(value = "fieldIds",required = false) String fieldIds
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("nameChar", nameChar);
            pageData.put("name", name);
            pageData.put("img", img);
            pageData.put("organizationId", organizationId);
            pageData.put("job", job);
            pageData.put("ranking", ranking);
            pageData.put("edu", edu);
            pageData.put("report", report);
            pageData.put("paper", paper);
            pageData.put("book", book);
            pageData.put("fieldIds", fieldIds);

            professorService.updateProfessor(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteProfessor")
    @ApiOperation(value = "删除专家")
    public JsonResult deleteProfessor(
            @ApiParam(value = "专家ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);

            professorService.deleteProfessor(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/disableOrEnableProfessor")
    @ApiOperation(value = "禁用或启用专家")
    public JsonResult disableOrEnableProfessor(
            @ApiParam(value = "专家ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            professorService.disableOrEnableProfessor(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listProfessor")
    @ApiOperation(value = "获取专家列表")
    public JsonResult listProfessor(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(professorService.listProfessor(pageData));
            result.setResultNum(professorService.listProfessorCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/addResearchField")
    @ApiOperation(value = "新增专家领域")
    public JsonResult addResearchField(
            @ApiParam(value = "专家领域,多个用逗号分隔", required = true) @RequestParam("names") String names
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("names", names);

            professorService.addResearchField(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateResearchField")
    @ApiOperation(value = "更新专家领域")
    public JsonResult updateResearchField(
            @ApiParam(value = "专家领域ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "专家领域名称", required = true) @RequestParam("name") String name
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("name", name);

            int count = professorService.getResearchFieldByName(pageData);
            if (count > 0) {
                result.setFailure("领域名称重复");
                return result;
            }

            professorService.updateResearchField(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteResearchField")
    @ApiOperation(value = "删除专家领域")
    public JsonResult deleteResearchField(
            @ApiParam(value = "专家领域ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            professorService.deleteResearchField(pageData);
            result.setSuccessful();
        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/disableOrEnableResearchField")
    @ApiOperation(value = "禁用或启用专家")
    public JsonResult disableOrEnableResearchField(
            @ApiParam(value = "ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            professorService.disableOrEnableResearchField(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listResearchField")
    @ApiOperation(value = "获取专家领域列表")
    public JsonResult listResearchField(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(professorService.listResearchField(pageData));
            result.setResultNum(professorService.listResearchFieldCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listResearchFieldAll")
    @ApiOperation(value = "获取专家领域列表")
    public JsonResult listResearchFieldAll(
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);

            result.setResult(professorService.listResearchFieldAll(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }




}
